var windowName = "user";
var Page;
apiready = function(){

    // onWindowShow(function () {
    //     getInfo(Page)
    // })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        //页面数据
        user:{
            id:"0",
            name:"未登录",
            phone:"登录/注册",
            headPic:picUserHead,
            grade:"",
        }
        ,login:"0"
        ,isShop:0
        ,live:[]
    },{
        //页面方法
        "init":function () {
            //检查登录状态
            Page.login = getLocData("login")
            var user = getLocData("user");
            logJson(user,"user");
            if (isTrue(user)) {
                Page.user = user;
            }
            // getInfo(Page)
        }
        ,clearCache:function () {
            delAllLocData()
            goto('login')
        }
        , openShop: function () {
            apiPost("/app/open/shop",{},function (res,code) {
                if (code == 200) {
                    showToast(res.message)
                    setTimeout(function () {
                        goto("shopHome")
                    },500)
                }

            })
        }
        ,showToast:showToast
    })
    Page.init()

}
function updateWinData() {
    //检查登录状态
    Page.login = getLocData("login")
    var user = getLocData("user");
    logJson(user,"user");
    logJson(Page.login,"user Page.login");
    if (isTrue(Page.login)) {
        getInfo(Page)
    }
    if (isTrue(user)) {
        Page.user = user;
    }else {
        Page.user = {
            id:"0",
            name:"未登录",
            phone:"登录/注册",
            headPic:picUserHead,
            grade:"",
        }
    }
}

function getInfo(_this) {
    apiGet("/app/user/home",{},function (res) {
        logJson(res)
        if (isTrue(res.data)) {
            Page.user = res.data
            if (isTrue(Page.user.shop)) {
                Page.isShop = 1;
            }else {
                Page.isShop = 0;
            }
            setLocData('user',_this.user);

            if (res.data.phone == null) {
                goto("fillPhone")
            }
        }

    })
}
